﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Timers;
using System.Windows.Forms;

namespace WindowsFormsApp2 {
  public partial class Form1 : Form {
    private NotifyIcon notifyIcon;
    private Icon originalIcon;
    private List<Icon> rotatedIcons;
    private System.Timers.Timer rotationTimer;
    private int currentIconIndex = 0;
    private ContextMenuStrip contextMenuStrip;

    public Form1() {
      InitializeComponent();

      // 加载原始图标
      originalIcon = new Icon("shell32_320.ico");

      // 创建 NotifyIcon 控件
      notifyIcon = new NotifyIcon();
      notifyIcon.Visible = true;

      // 生成旋转后的图标帧
      GenerateRotatedIcons();

      // 初始化 NotifyIcon 的图标
      notifyIcon.Icon = rotatedIcons[0];

      // 创建定时器
      rotationTimer = new System.Timers.Timer(75);
      rotationTimer.Elapsed += RotationTimer_Elapsed;
      rotationTimer.AutoReset = true;
      // 创建 ContextMenuStrip 并添加菜单项
      contextMenuStrip = new ContextMenuStrip();
      ToolStripMenuItem startMenuItem = new ToolStripMenuItem("开始");
      startMenuItem.Click += StartMenuItem_Click;
      ToolStripMenuItem stopMenuItem = new ToolStripMenuItem("停止");
      stopMenuItem.Click += StopMenuItem_Click;
      ToolStripMenuItem exitMenuItem = new ToolStripMenuItem("退出");
      exitMenuItem.Click += ExitMenuItem_Click;
      contextMenuStrip.Items.Add(startMenuItem);
      contextMenuStrip.Items.Add(stopMenuItem);
      contextMenuStrip.Items.Add(exitMenuItem);

      // 将 ContextMenuStrip 关联到 NotifyIcon
      notifyIcon.ContextMenuStrip = contextMenuStrip;

      this.Load += Form1_Load;
    }

    private void Form1_Load(object sender, EventArgs e) {
      this.ShowInTaskbar = false;
      this.Hide();
      if (!rotationTimer.Enabled) {
        rotationTimer.Start();
      }
    }

    private void GenerateRotatedIcons() {
      rotatedIcons = new List<Icon>();
      int rotationStep = 15;
      for (int angle = 0; angle < 360; angle += rotationStep) {
        rotatedIcons.Add(RotateIcon(originalIcon, angle));
      }
    }

    private Icon RotateIcon(Icon icon, int angle) {
      Bitmap bitmap = icon.ToBitmap();
      Bitmap rotatedBitmap = new Bitmap(bitmap.Width, bitmap.Height);

      using (Graphics g = Graphics.FromImage(rotatedBitmap)) {
        g.SmoothingMode = SmoothingMode.AntiAlias;
        g.InterpolationMode = InterpolationMode.HighQualityBicubic;
        g.PixelOffsetMode = PixelOffsetMode.HighQuality;

        g.TranslateTransform(bitmap.Width / 2, bitmap.Height / 2);
        g.RotateTransform(angle);
        g.TranslateTransform(-bitmap.Width / 2, -bitmap.Height / 2);

        g.DrawImage(bitmap, 0, 0);
      }
      return Icon.FromHandle(rotatedBitmap.GetHicon());
    }

    private void RotationTimer_Elapsed(object sender, ElapsedEventArgs e) {
      this.Invoke((MethodInvoker)delegate
      {
        currentIconIndex = (currentIconIndex + 1) % rotatedIcons.Count;
        notifyIcon.Icon = rotatedIcons[currentIconIndex];
      });
    }

    private void StartMenuItem_Click(object sender, EventArgs e) {
      if (!rotationTimer.Enabled) {
        rotationTimer.Start();
      }
    }

    private void StopMenuItem_Click(object sender, EventArgs e) {
      if (rotationTimer.Enabled) {
        rotationTimer.Stop();
        //// 复位图标索引到初始位置
        //currentIconIndex = 0;
        //// 更新图标为初始状态
        //notifyIcon.Icon = rotatedIcons[currentIconIndex];
      }
    }

    private void ExitMenuItem_Click(object sender, EventArgs e) {
      // 释放资源
      notifyIcon.Visible = false;
      notifyIcon.Dispose();
      originalIcon.Dispose();
      foreach (Icon rotatedIcon in rotatedIcons) {
        rotatedIcon.Dispose();
      }
      rotationTimer.Stop();
      rotationTimer.Dispose();
      contextMenuStrip.Dispose();

      // 关闭应用程序
      Application.Exit();
    }

    protected override void OnFormClosing(FormClosingEventArgs e) {
      base.OnFormClosing(e);
      // 释放资源
      notifyIcon.Visible = false;
      notifyIcon.Dispose();
      originalIcon.Dispose();
      foreach (Icon rotatedIcon in rotatedIcons) {
        rotatedIcon.Dispose();
      }
      rotationTimer.Stop();
      rotationTimer.Dispose();
    }
  }
}